package com.atguigu.exam;

import java.io.*;

/**
 * 利用程序复制 test.txt 为 test2.txt
 * @author xbd
 * @create 2021-05-30 16:26
 */
public class CopyTxtTest {
    public static void main(String[] args) {
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            bis = new BufferedInputStream(new FileInputStream("test.txt"));
            bos = new BufferedOutputStream(new FileOutputStream("test2.txt"));

            byte[] bytes = new byte[10];
            int len;
            while ((len = bis.read(bytes)) != -1){
                bos.write(bytes,0,len);
                bos.flush();
            }
            System.out.println("复制成功");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(bis != null){
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(bos != null){
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
